package controller
{
	import flash.events.EventDispatcher;
	import json.JSON;
	import mx.controls.Alert;
	public class BaseController  extends EventDispatcher
	{
		public static var PLATFORM:int=0;
		/*
		*玩家消息
		**/
		public static var PLAYER:int=1;	
		/*
		*斗地主消息
		*/
		public static var FIGHT_LANDLORD:int=3;
		// static instance 
		protected static var instance:BaseController = null;
		//为了提前实例化单列，
		public function init():void{
			
		}
		protected var result:Object = new Object();
		public function BaseController()
		{
			if ( instance != null )
				throw new Error( "Only one Singleton instance should be instantiated" ); 
//			SocketManager.Instance.init();
		}
		public function CreateOutMessage(num:Number,signMethod:String,msg:String):String
		{
			var thisFrame:Date = new Date();
		
		var msg:String=num.toString()+"~"+thisFrame.getTime()+"~"+signMethod+"~"+msg;
			trace("发送请求消息:"+msg);
			return msg;
		}
		static public function get Instance():BaseController
		{
			if ( instance == null )
				instance = new BaseController();
			return instance;
		}
		public function receive(results:Array):Boolean
		{
			try{
				result = json.JSON.decode(results[3]);
			}catch(error:ArgumentError){
				trace("json decode error");
				controller.ControllerManager.Instance.debug("处理派发消息失败，接收到派发消息："+results[2]+"，json消息："+results[3],true);
			}
			if(result.hasOwnProperty("success"))
			{
				if(!result.success)
				{						
					if(result.hasOwnProperty("message")){
						LobyErrorState.Instance.showErrMsg(result.message);
					}
					trace("处理Json消息失败 :"+results[1]+":："+results[2]+".消息内容："+results[3]);
					return false;
				}
			}	
			trace("处理Json消息成功 ：:"+results[1]+":："+results[2]+".消息内容："+results[3]);
			return true;
		}
	}
}